//
//  CZToTweetViewController.swift
//  GZWeibo007
//
//  Created by OY on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD
enum CZToTweetViewControllerType: Int {
    case Compose = 1
    case Repost = 2
    case Comment = 3
}
class CZToTweetViewController: UIViewController {
    
    //微博模型
    var staus :CZStatus?
    //控制器类型
    private var controllerType : CZToTweetViewControllerType
    //构造函数
    init(controllerType : CZToTweetViewControllerType)
    {
        self.controllerType = controllerType
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view .backgroundColor = UIColor.whiteColor()
        prepareUI()
    }
    // MARK : - 准备UI
    
    func  prepareUI()
    {
        setNavigationTaBar()
        setTextView()
    }
    // 设置导航
    private func  setNavigationTaBar()
    {
        //左边
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "cancel")
        //右边
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendWeibo")
        setNavigationTitle()
    }
    // 设置导航标题
    private func setNavigationTitle()
    {
        var  frontEnd = "发微博"
        if controllerType == CZToTweetViewControllerType.Comment
        {
            frontEnd = "转发微博"
            
        } else if controllerType == CZToTweetViewControllerType.Repost
        {
            frontEnd = "评论微博"
        }
        //获取用户名
        if let userName = CZUserAccount.loadUserAccount()?.screen_name{
            
            let titleName = frontEnd + "\n" + userName
            // 创建可变的属性文本
            let attrString = NSMutableAttributedString(string: titleName)
            let  label = UILabel()
            //换行
            label.numberOfLines = 0
            //居中
            label.textAlignment = NSTextAlignment.Center
            
            // 获取NSRange
            let nameRange = (titleName as NSString).rangeOfString(userName)
            // 设置属性文本的属性
            attrString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(12), range: nameRange)
            attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGrayColor(), range: nameRange)
            label.font = UIFont.systemFontOfSize(14)
            label.attributedText = attrString
            label.sizeToFit()
            navigationItem.titleView = label
        }
    }
    //添加textView到View上
    private func setTextView()
    {
        view.addSubview(textView)
        textView.ff_Fill(view)
    }
    
    // //移除控制器
    func cancel()
    {
        SVProgressHUD .dismiss()
        dismissViewControllerAnimated(true, completion: nil)
        //移除控制器同时 去掉键盘
        textView.resignFirstResponder()
    }
    //发送微博
    func sendWeibo()
    {
        let text = textView.text
        if text == nil || text == ""
        {
            SVProgressHUD.showErrorWithStatus("发送的内容不能为空")
            return
        }
        if controllerType == CZToTweetViewControllerType.Compose
        {
            SVProgressHUD.showInfoWithStatus("正在发布中！请稍后...", maskType: SVProgressHUDMaskType.Black)
            //发送微博
            //MARK: - 没有网络数据 有了在做
            // 发送微博
            CZNetworkTool.sharedInstance.sendStatus(text!, finished: { (result, error) -> () in
                if error != nil {
                    print("error:\(error)")
                    SVProgressHUD.showErrorWithStatus("网络不给力...", maskType: SVProgressHUDMaskType.Black)
                    return
                }

                
                
                
                print("发布微博成功:\(result)")
                
                // 发送成功, 直接关闭界面
                self.cancel()

            })
                
            
            }
//        } else if controllerType == CZToTweetViewControllerType.Repost {
//            // 显示正在发送
//            SVProgressHUD.showWithStatus("正在转发微博...", maskType: SVProgressHUDMaskType.Black)
//            
//            CZNetworkTool.sharedInstance.repostStatus(staus!.id, status: text, finished: { (result, error) -> () in
//                if error != nil {
//                    print("error:\(error)")
//                    SVProgressHUD.showErrorWithStatus("网络不给力...", maskType: SVProgressHUDMaskType.Black)
//                    return
//                }
//                
//                // 发送成功, 直接关闭界面
//                print("转发微博成功:\(result)")
//                self.close()
//            })
//        } else if controllerType == CZToTweetViewControllerType.Comment {
//            // 显示正在发送
//            SVProgressHUD.showWithStatus("正在评论微博...", maskType: SVProgressHUDMaskType.Black)
//            
//            CZNetworkTool.sharedInstance.commentStatus(staus!.id, comment: text, finished: { (result, error) -> () in
//                if error != nil {
//                    print("error:\(error)")
//                    SVProgressHUD.showErrorWithStatus("网络不给力...", maskType: SVProgressHUDMaskType.Black)
//                    return
//                }
//                
//                // 发送成功, 直接关闭界面
//                print("评论微博成功:\(result)")
//                self.close()
//            })
//        }
    }

            
    
        
    
    // MARK: -懒加载
    private lazy var textView : UITextView =
    {
        let  textView = UITextView()
        
        textView.font = UIFont.systemFontOfSize(18)
        textView.textColor = UIColor(white: 0.85, alpha: 1)
        textView.backgroundColor = UIColor.orangeColor()
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        textView.bounces = true
        textView.alwaysBounceVertical = true
        return textView
        
    }()
    
    
}
